package com.aibili.common;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import com.daoshun.common.DsConstant;

public class SMessageUtil {

	public static void main(String[] args) {
//		String uri = "http://send.18sms.com/msg/HttpBatchSendSM";// 应用地址
//		String account = "lx15057163197";// 账号
//		String pswd = "Sy123456";// 密码
		String mobiles = "15757129925";// 手机号码，多个号码使用","分割
		String content = "您好，您的验证码：xxxx，5分钟内有效";// 短信内容 注意内容中的逗号请使用中文状态下的逗号
//		boolean needstatus = true;// 是否需要状态报告，需要true，不需要false
//		String product = "";// 产品ID(不用填写)
//		String extno = "";// 扩展码(不用填写)
		try {
			String returnString = batchSend(mobiles, content);
			//System.out.println(returnString);
			// TODO 处理返回值,参见HTTP协议文档
		} catch (Exception e) {
			// TODO 处理异常
			e.printStackTrace();
		}
	}
	
	public static String batchSend(String mobiles, String content) throws Exception {
		
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod();
		String s = "";
		URI base = new URI(DsConstant.SHIYUAN_URL, false);
		method.setURI(new URI(base, "HttpBatchSendSM", false));
		NameValuePair[] params = new NameValuePair[] {
				new NameValuePair("account", DsConstant.SHIYUAN_ACCOUNT), 
				new NameValuePair("pswd", DsConstant.SHIYUAN_PASSWORD), 
				new NameValuePair("mobile", mobiles), 
				new NameValuePair("msg", content), 
				new NameValuePair("needstatus", String.valueOf(true)), 
				new NameValuePair("product", ""), 
				new NameValuePair("extno", "")
		};
		method.setQueryString(params);
		
		int result = client.executeMethod(method); 
		//System.out.println(result);
		InputStream is = method.getResponseBodyAsStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		for (int len = 0; (len = is.read(buffer)) != -1;) {
			out.write(buffer, 0, len);
		}
		s = URLDecoder.decode(out.toString(), "UTF-8");
		method.releaseConnection();
		
		return s;
	}
}

